/*
  This file is part of SPARK: SharePoint Application Resource Kit.
  The project is distributed via CodePlex: http://www.codeplex.com/spark/
  Copyright (C) 2003-2009 by Thomas Carpe. http://www.ThomasCarpe.com/

  SPARK is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  SPARK is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with SPARK.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace Behemoth.Web.UI.WebControls {

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

	public class WebControlTools {

		/// <summary>
		/// Used to scan for controls within a hierarchy of control containers. The
		/// </summary>
		/// <param name="root">The top levle control at which to begin the search</param>
		/// <param name="id">The ID or ClientOD property to search for</param>
		/// <param name="resolveByClientId">
		/// Set to true if you want to find a control whose CLientID property
		/// ends with a specific string. Useful for finding a specific control
		/// buried deep inside nested templates, mostly for research.</param>
		/// <returns></returns>
		public static Control RecurseFindControl( Control root, string id, bool resolveByClientId ) {
			if (resolveByClientId) {
				if (root.ClientID.EndsWith( id ))
					return root;
				if (root.Controls == null)
					return null;
				foreach (Control subCtl in root.Controls) {
					Control found = RecurseFindControl( subCtl, id, resolveByClientId );
					if (found != null)
						return found;
				}
			} else {
				Control ctl = root.FindControl( id );
				if (ctl != null)
					return ctl;
				if (root.Controls == null)
					return null;
				foreach (Control subCtl in root.Controls) {
					Control found = RecurseFindControl( subCtl, id, resolveByClientId );
					if (found != null)
						return found;
				}
			}
			return null;
		}
		public static Control RecurseFindControl( Control root, string id ) {
			return RecurseFindControl( root, id, false );
		}

		/// <summary>
		/// Gets the first child control that matches the specified type.
		/// This is useful for finding tempalted controls when you know
		/// the type but the ID is determined dynamically at run time.
		/// </summary>
		/// <param name="parent">The parent control to search</param>
		/// <param name="controlType">The type of control to search for</param>
		/// <param name="skipHowMany">An optional parameter to tell the search to skip the first x numbe rof controlls of this type that it finds, default is 0</param>
		/// <returns>A control of the specified type, or null if none found</returns>
		public static Control GetChildControlByType( Control parent, Type controlType, int skipHowMany ) {
			int i = 0;
			foreach (Control ctl in parent.Controls) {
				if (ctl.GetType() == controlType) {
					if (i++ >= skipHowMany)
						return ctl;
				}
			}
			return null;
		}
		public static Control GetChildControlByType( Control parent, Type controlType ) {
			return GetChildControlByType( parent, controlType, 0 );
		}

	} // class

} // namesapce
